<template>
  <div id="cart-bottom-bar">
    <div class="cheack-all">
      <CheckButton
        :isChecked="isCheckedAll"
        class="check-button"
        @click.native="checkClick"
      ></CheckButton>
      <span>全选</span>
    </div>
    <div class="total-price">
      合计：<span>￥{{ totalPrice }}</span>
    </div>
    <div class="calculate">结算({{ checkedLength }})</div>
  </div>
</template>

<script>
import CheckButton from "@/components/content/checkButton/CheckButton.vue";
export default {
  props: {},
  data() {
    return {};
  },
  computed: {
    // 购物车商品总价
    totalPrice() {
      return this.$store.state.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return parseFloat(
            (preValue += item.price.slice(1) * item.count).toFixed(2)
          );
        }, 0);
    },
    checkedLength() {
      return this.$store.state.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return (preValue += item.count);
        }, 0);
    },
    isCheckedAll() {
      if (this.$store.state.cartList.length === 0) {
        return false;
      } else {
        return (
          this.$store.state.cartList.filter((item) => !item.checked).length ===
          0
        );
      }
    },
  },
  components: {
    CheckButton,
  },
  methods: {
    checkClick() {
      if (this.isCheckedAll) {
        this.$store.state.cartList.forEach((item) => (item.checked = false));
      } else {
        this.$store.state.cartList.forEach((item) => (item.checked = true));
      }
    },
  },
};
</script>

<style lang="less" scoped>
#cart-bottom-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  height: 40px;
  background-color: #fff;
  .cheack-all {
    display: flex;
    width: 80px;
    justify-content: space-evenly;
    align-items: center;
  }
  .total-price {
    margin-left: 20px;
    span {
      font-size: 20px;
      color: #ff6c37;
    }
  }
  .calculate {
    height: 35px;
    width: 100px;
    border-radius: 17.5px;
    text-align: center;
    line-height: 40px;
    color: #fff;
    background-image: linear-gradient(to right, #ff8198, #ff5777);
  }
}
</style>